﻿using System;
using System.Xml.Linq;

namespace Twinkle.Tweak
{
    public class TStatus : Krile.Kernel.Data.Status
    {
        private string userId = null;

        public override Krile.Kernel.Data.User User
        {
            get
            {
                return Kernel.Storage.LookupUser(userId);
            }
            set
            {
                base.User = value;
                userId = value.Id;
            }
        }

        public static TStatus CreateByNode(XElement sNode)
        {
            if (sNode == null || sNode.Element("id") == null)
                return null;
            var status = new TStatus();
          
            status.Id = sNode.Element("id").Value;

            status.Truncated = sNode.Element("truncated").ParseBool();

            status.Text = new Krile.Kernel.Control.XString();
            status.Text.Original = sNode.Element("text").ParseString();
            status.CreatedAt = sNode.Element("created_at").ParseDateTime("ddd MMM d HH':'mm':'ss zzz yyyy");

            status.Source = sNode.Element("source").ParseString();
            status.Favored = sNode.Element("favorited").ParseBool();
            var rid = sNode.Element("in_reply_to_status_id").ParseString();
            if (!String.IsNullOrEmpty(rid))
            {
                status.ReplyData = new TReplyData(
                    rid,
                    sNode.Element("in_reply_to_screen_name").ParseString());
            }
            else if(Kernel.Config.Behavior.TreatAsReplyToMeIfMentionToMe &&
                    Kernel.Config.Accounts != null && Kernel.Config.Accounts.Length > 0)
            {
                foreach (var a in Kernel.Config.Accounts)
                {
                    if (status.Text.Original.Contains("@" + a.UserId))
                    {
                        status.ReplyData = new TReplyData(
                            null, a.UserId);
                        break;
                    }
                }
            }
            if (sNode.Element("retweeted_status") != null)
                status.StatusKind = StatusKinds.Retweet;
            else
                status.StatusKind = StatusKinds.Normal;

            status.userId = TUser.GetUserIDByNode(sNode.Element("user"));
            var unode = TUser.CreateByNode(sNode.Element("user"));
            if (unode != null)
                Kernel.Storage.RegistUser(unode);
            return status;
        }

        public static TStatus CreateByDMNode(XElement dmNode)
        {
            if (dmNode == null || dmNode.Element("id") == null)
                return null;
            var status = new TStatus();

            status.Id = dmNode.Element("id").Value;
            status.Text = new Krile.Kernel.Control.XString();
            status.Text.Original = dmNode.Element("text").ParseString();
            status.CreatedAt = dmNode.Element("created_at").ParseDateTime("ddd MMM d HH':'mm':'ss zzz yyyy");

            status.StatusKind = StatusKinds.DirectMessage;
            status.userId = dmNode.Element("sender_screen_name").ParseString();

            status.ReplyData = new Krile.Kernel.Data.ReplyData();
            status.ReplyData.ReplyToUserId = dmNode.Element("recipient_screen_name").ParseString();
            status.Unfavorable = true;

            var senderU = TUser.CreateByNode(dmNode.Element("sender"));
            if (senderU != null)
                Kernel.Storage.RegistUser(senderU);
            var receipientU = TUser.CreateByNode(dmNode.Element("recipient"));
            if (receipientU != null)
                Kernel.Storage.RegistUser(receipientU);
            return status;
        }

        public static string GetId(XElement node)
        {
            if (node == null ||  node.Element("id") == null)
                return null;
            return node.Element("id").Value;
        }

        public TStatus() : base() { }

        public enum StatusKinds { Normal, Retweet, DirectMessage }
        /// <summary>
        /// Kind of status
        /// </summary>
        public StatusKinds StatusKind = StatusKinds.Normal;

        public bool Truncated { get; set; }

        public string Source { get; set; }

        public override Uri StatusPage
        {
            get
            {
                return new Uri("http://twitter.com/" + this.userId + "/status/" + this.Id);
            }
            set { }
        }

        public override System.Drawing.Color OverrideBackColor
        {
            get
            {
                if (this.StatusKind == StatusKinds.DirectMessage)
                    return Kernel.Config.StatusDesign.DMBackColor;
                else
                    return base.OverrideBackColor;
            }
            set
            {
                base.OverrideBackColor = value;
            }
        }

        public override System.Drawing.Color OverrideForeColor
        {
            get
            {
                if (this.StatusKind == StatusKinds.DirectMessage)
                    return Kernel.Config.StatusDesign.DMTextColor;
                else
                    return base.OverrideForeColor;
            }
            set
            {
                base.OverrideForeColor = value;
            }
        }

        public override bool Destroyable
        {
            get
            {
                if (Kernel.Config.Accounts == null)
                    return false;
                foreach (var acc in Kernel.Config.Accounts)
                {
                    if (acc.UserId == this.User.Id)
                    {
                        return true;
                    }
                }
                return false;
            }
        }
    }
}
